#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using System;
using Cysharp.Threading.Tasks;
using Neatly.Module;
using UnityEngine;

namespace Neatly
{
    public class NeatlyFrame : Singleton<NeatlyFrame>
    {
        //游戏根节点
        public GameObject gameRoot { get; private set; }
        //音频节点
        public GameObject audioRoot { get; private set; }
        private bool m_Initialized;

        // 初始化框架
        public async UniTask Launch()
        {
            await ModuleManager.Instance.InitModules();
            m_Initialized = true;
        }

        //登录加载
        public void LoginLoad()
        {
            ModuleManager.Instance.LoginLoad();
        }

        public void Update()
        {
            if (!m_Initialized)
            {
                return;
            }
            ModuleManager.Instance.Update();
        }

        public void Dispose()
        {
            ModuleManager.Instance.Dispose();
            DestroyInstance();
        }

        #region Setting API

        public void SetGameRoot(GameObject root)
        {
            gameRoot = root;
        }

        public void SetAudioRoot(GameObject root)
        {
            audioRoot = root;
        }
        #endregion

        #region Module API
        public void AddModule(ModuleBase module)
        {
            ModuleManager.Instance.Add(module);
        }

        public void AddModule<T>() where T : ModuleBase, new()
        {
            ModuleManager.Instance.Add<T>();
        }

        public void AddManager<T>() where T : Component
        {
            ManagerSingleton<T>.Init();
        }

        public void RemoveModule(string moduleName)
        {
            ModuleManager.Instance.Remove(moduleName);
        }

        public void RemoveModule<T>() where T : ModuleBase, new()
        {
            ModuleManager.Instance.Remove<T>();
        }

        public ModuleBase GetModule(string name)
        {
            return ModuleManager.Instance.Get(name);
        }

        public T GetModule<T>() where T : ModuleBase
        {
            return ModuleManager.Instance.Get<T>();
        }

        public Action<string> DestroyAction { get; private set; }
        
        public void RegisterDestroyAction(Action<string> action)
        {
            DestroyAction = action;
        }
        #endregion
    }
}
